package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Encyclopedia;

public class GenericEncyclopediaDAO extends AutoKeys implements
		org.jetnuke.dao.EncyclopediaDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericEncyclopediaDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaModel#retrieveByPK(int)
	 */
	public Encyclopedia retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("EncyclopediaModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Encyclopedia s = new Encyclopedia();

		if (rs.next()) {
			fillEncyFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillEncyFromResultSet(Encyclopedia s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillEncyFromResultSet(Encyclopedia, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setACTIVE(rs.getInt("ACTIVE"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setEID(rs.getInt("EID"));
		s.setELANGUAGE(rs.getString("ELANGUAGE"));
		s.setTITLE(rs.getString("TITLE"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillEncyFromResultSet(Encyclopedia, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Encyclopedia u = new Encyclopedia();
			fillEncyFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("EncyclopediaModel.selectAll"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaModel#selectAllActive()
	 */
	public List selectAllActive() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActive() - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("EncyclopediaModel.selectAllActive"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActive() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaModel#addNew(org.jetnuke.entities.Encyclopedia)
	 */
	public int addNew(Encyclopedia ency) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Encyclopedia) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("EncyclopediaModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("EncyclopediaModel.lastGeneratedEncyID"));
		initEncy(ency, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Encyclopedia) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param ency
	 * @param p
	 */
	private void initEncy(Encyclopedia ency, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initEncy(Encyclopedia, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, ency.getACTIVE());
		p.setString(2, ency.getDESCRIPTION());
		p.setString(3, ency.getELANGUAGE());
		p.setString(4, ency.getTITLE());

		if (logger.isDebugEnabled()) {
			logger.debug("initEncy(Encyclopedia, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaModel#update(org.jetnuke.entities.Encyclopedia)
	 */
	public void update(Encyclopedia ency) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Encyclopedia) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("EncyclopediaModel.update");
		p.setInt(1, ency.getACTIVE());
		p.setString(2, ency.getDESCRIPTION());
		p.setString(3, ency.getELANGUAGE());
		p.setString(4, ency.getTITLE());
		p.setInt(5, ency.getEID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Encyclopedia) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaModel#selectAllInactive()
	 */
	public List selectAllInactive() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllInactive() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("EncyclopediaModel.selectAllInactive"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllInactive() - end"); //$NON-NLS-1$
		}
		return list;
	}

}
